package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Logout implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/**
	 * 此类包括了退出删除session和cookie功能
	 * 也包括了识别cookie自动登录功能
	 * 
	 * 自动登录功能在后期加入，所以命名不规范
	 */
	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpSession session = request.getSession();

		String k = (String)request.getParameter("k");
		
		if(k != null && k.equals("logout")){
			
			//退出  清除session和cookie
			clearCookieAndSession(arg1, session);
		}else{
			
			// 直接访问登录界面，若存在登录cookie，则自动登录
			autoLogin(arg1, request);
		}
		chain.doFilter(arg0, arg1);
	}

	/**---------------------------------
	 * 		通过cookie自动登录  ver.12.07
	 * ---------------------------------
	 * @param arg1
	 * @param request
	 * @throws IOException
	 * 
	 * 先获取cookie，如果存在cookie
	 * 遍历cookie获得账号和密码（已加密）
	 * 把账号信息提交到checkIdentity
	 * 登录
	 */
	private void autoLogin(ServletResponse arg1, HttpServletRequest request)
			throws IOException {
		Cookie cookie = null;
		Cookie[] cookies = request.getCookies();
		String id_c = null;
		String pw_c = null;
		String idr_c = null;

		if(cookies != null)
		{
			for(int n = 0; n<cookies.length; ++n){
				cookie = cookies[n];
				if(cookie.getName().equals("id")){
					id_c = cookie.getValue();
				}else
					if(cookie.getName().equals("pw")){
						pw_c = cookie.getValue();
					}else
						if(cookie.getName().equals("identity")){
							idr_c = cookie.getValue();
						}
			}

			if(id_c != null){
				((HttpServletResponse)arg1).sendRedirect("checkIdentity?id="+id_c+"&pw="+pw_c+"&identity_radio="+idr_c+"&auto=true");
			}
		}
	}

	/**
	 * ------------------------------------
	 * 		清除session和cookie  ver.12.07
	 * ------------------------------------
	 * 
	 * @param arg1
	 * @param session
	 * 
	 * 把cookie的过期时间设置为0，即现在过期
	 * 调用invalidate清除session
	 */
	private void clearCookieAndSession(ServletResponse arg1, HttpSession session) {
		session.invalidate();
		Cookie cookie_id = new Cookie("id", null);
		Cookie cookie_pw = new Cookie("pw", null);
		Cookie cookie_idr = new Cookie("identity", null);
		cookie_id.setMaxAge(0); // 清除cookie
		cookie_pw.setMaxAge(0);
		cookie_idr.setMaxAge(0);

		HttpServletResponse response = (HttpServletResponse)arg1;
		response.addCookie(cookie_pw);
		response.addCookie(cookie_id);
		response.addCookie(cookie_idr);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

}
